package com.atguigu.tingshu.payment.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.account.RechargeInfo;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.model.payment.PaymentInfo;
import com.atguigu.tingshu.payment.mapper.PaymentInfoMapper;
import com.atguigu.tingshu.payment.service.PaymentInfoService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wechat.pay.java.service.partnerpayments.jsapi.model.Transaction;
import org.springframework.stereotype.Service;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements PaymentInfoService {

    @Override
    public void savePaymentInfo(String payType, String orderNo, OrderInfo orderInfo) {
        PaymentInfo paymentInfo = this.getOne(Wrappers.lambdaQuery(PaymentInfo.class)
                .eq(PaymentInfo::getOrderNo, orderNo)
                .eq(PaymentInfo::getUserId, AuthContextHolder.getUserId()));
        if(paymentInfo!=null){
            throw new GuiguException(ResultCodeEnum.PAYMENT_SUBMIT_REPEAT);
        }
        paymentInfo = new PaymentInfo();
        paymentInfo.setUserId(AuthContextHolder.getUserId());
        paymentInfo.setPaymentType(payType);
        paymentInfo.setOrderNo(orderNo);
        paymentInfo.setPayWay(orderInfo.getPayWay());
        paymentInfo.setAmount(orderInfo.getOrderAmount());
        paymentInfo.setContent(orderInfo.getOrderTitle());
        //使用1401状态
        paymentInfo.setPaymentStatus(SystemConstant.PAYMENT_STATUS_UNPAID);
        // 还可以在数据库 paymentinfo表中 设置 orderNo唯一键约束   保证幂等性
        this.save(paymentInfo);
    }

    @Override
    public void updatePaymentInfoStatus(Transaction transaction, String number) {
        this.update(Wrappers.lambdaUpdate(PaymentInfo.class)
                .eq(PaymentInfo::getOrderNo , transaction.getOutTradeNo())
                .set(PaymentInfo::getPaymentStatus , number)
                .set(PaymentInfo::getCallbackTime , transaction.getSuccessTime())
                .set(PaymentInfo::getCallbackContent , JSONObject.toJSONString(transaction)));

    }

    @Override
    public void savePaymentInfo(String payType, String orderNo, RechargeInfo rechargeInfo) {
        PaymentInfo paymentInfo = this.getOne(Wrappers.lambdaQuery(PaymentInfo.class)
                .eq(PaymentInfo::getOrderNo, orderNo)
                .eq(PaymentInfo::getUserId, rechargeInfo.getUserId()));
        if(paymentInfo!=null){
            throw new GuiguException(ResultCodeEnum.PAYMENT_SUBMIT_REPEAT);
        }
        paymentInfo = new PaymentInfo();
        paymentInfo.setUserId(rechargeInfo.getUserId());
        paymentInfo.setPaymentType(payType);
        paymentInfo.setOrderNo(orderNo);
        paymentInfo.setPayWay(rechargeInfo.getPayWay());
        paymentInfo.setAmount(rechargeInfo.getRechargeAmount());
        paymentInfo.setContent("充值："+rechargeInfo.getRechargeAmount()+" 元");
        //使用1401状态
        paymentInfo.setPaymentStatus(SystemConstant.PAYMENT_STATUS_UNPAID);
        // 还可以在数据库 paymentinfo表中 设置 orderNo唯一键约束   保证幂等性
        this.save(paymentInfo);
    }


}
